package android.video.online.ui.classify;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.video.online.R;
import android.video.online.adapter.ClassifyAdapter;
import android.video.online.core.BasicPresenter;
import android.video.online.model.Category;
import android.video.online.model.CourseModel;
import android.video.online.ui.BasicActivity;
import android.video.online.ui.BasicFragment;
import android.video.online.widget.LoadingView;
import android.video.online.widget.couldtag.TagBaseAdapter;
import android.video.online.widget.couldtag.TagCloudLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created by Jay on 2017/8/13.
 */

public class ClassifyFragment extends BasicFragment implements ClassifyContract.View {

    private LoadingView mLoadingView;
    private RecyclerView mLvClassify;
    private ClassifyAdapter mClassifyAdapter;

    private TagCloudLayout mLvClazzContent;
    private TagBaseAdapter mContentAdapter;

    private ClassifyContract.Presenter presenter;
    private List<String> mList;
    private List<String> tList;

    private Category mCurrentCategory;

    @Override
    public int setLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    public void init() {
        initView();
        initData();
        initListener();
        presenter = new ClassifyPresenter(this);
        presenter.loadAllCategory(mLoadingView);
    }

    private void initListener() {

    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add("中华人名共和国");
        mList.add("大韩民国");
        mList.add("日本");
        mList.add("朝鲜");
        mList.add("台湾");
        mList.add("香港特别行政区");
        mList.add("澳门特别行政区");
        mList.add("越南");
        mList.add("老挝");
        mList.add("柬埔寨");
        mList.add("泰国");
        mList.add("缅甸");
        mList.add("马来西亚");
        mList.add("新加坡");
        mList.add("印度尼西亚");
        mList.add("文莱");
        mList.add("菲律宾");
    }

    private List<String> initTempData() {
        List<String> tList = new ArrayList<>();
        int count = new Random().nextInt(20) + 10;
        for (int i = 0; i < count; i++) {
            tList.add(mList.get(new Random().nextInt(mList.size())));
        }
        return tList;
    }

    private void initView() {
        mLoadingView = (LoadingView) parentLayout.findViewById(R.id.ld_loading);
        mLvClassify = (RecyclerView) parentLayout.findViewById(R.id.lv_clazz_title);
        mLvClazzContent = (TagCloudLayout) parentLayout.findViewById(R.id.lv_clazz_content);
    }

    @Override
    public BasicPresenter getPresenter() {
        return presenter;
    }

    @Override
    public void onFailed() {
        tList = initTempData();
        mContentAdapter = new TagBaseAdapter(getContext(), tList);
        mLvClazzContent.setAdapter(mContentAdapter);

    }

    @Override
    public void onSuccessView(List<Category> tData) {
        mClassifyAdapter = new ClassifyAdapter((BasicActivity) getActivity(), tData, presenter);
        RecyclerView.LayoutManager layoutManage = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        mLvClassify.setLayoutManager(layoutManage);
        mLvClassify.setAdapter(mClassifyAdapter);

        if (tData.size() > 0) {
            mCurrentCategory = tData.get(0);
        }
        presenter.loadCurrentCourse(mLoadingView, mCurrentCategory);
    }

    @Override
    public void onChangeContentView(Category category) {
        tList = initTempData();
        mContentAdapter = new TagBaseAdapter(getContext(), tList);
        mLvClazzContent.setAdapter(mContentAdapter);
    }

    @Override
    public void onSuccessCourseView(List<CourseModel> courseModels) {
        tList = new ArrayList<>();
        if (null != courseModels && courseModels.size() > 0) {
            tList.clear();
        }
        for (int i = 0; i < courseModels.size(); i++) {
            tList.add(courseModels.get(i).getTitle());
        }
        mContentAdapter = new TagBaseAdapter(getContext(), tList);
        mLvClazzContent.setAdapter(mContentAdapter);
    }
}
